<html>
<head><meta charset="utf-8"><title>Explicit move for structs · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Explicit.20move.20for.20structs.html">Explicit move for structs</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="167934410"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Explicit%20move%20for%20structs/near/167934410" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Papp <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Explicit.20move.20for.20structs.html#167934410">(Jun 12 2019 at 10:46)</a>:</h4>
<p>Is there a way to move a struct which implements the Copy trait? I know that structs that don't implement the trait are moved automatically, but how can I force this behavior for the ones that do?</p>



<a name="167935182"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Explicit%20move%20for%20structs/near/167935182" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Explicit.20move.20for.20structs.html#167935182">(Jun 12 2019 at 10:59)</a>:</h4>
<p>That's not possible</p>



<a name="167935324"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Explicit%20move%20for%20structs/near/167935324" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Explicit.20move.20for.20structs.html#167935324">(Jun 12 2019 at 11:01)</a>:</h4>
<p>what does it even mean to "move" something that is <code>Copy</code>?</p>



<a name="167940769"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Explicit%20move%20for%20structs/near/167940769" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Explicit.20move.20for.20structs.html#167940769">(Jun 12 2019 at 12:23)</a>:</h4>
<p>IOW, I don't understand the question.</p>



<a name="167940918"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Explicit%20move%20for%20structs/near/167940918" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Explicit.20move.20for.20structs.html#167940918">(Jun 12 2019 at 12:25)</a>:</h4>
<p>I think the question is whether a variable can be marked as "moved out of" or "not available anymore" so that any further accesses will error</p>



<a name="167953698"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Explicit%20move%20for%20structs/near/167953698" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Explicit.20move.20for.20structs.html#167953698">(Jun 12 2019 at 14:44)</a>:</h4>
<p>I suppose one could theoretically support <code>let x = move y;</code></p>



<a name="167953723"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Explicit%20move%20for%20structs/near/167953723" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Explicit.20move.20for.20structs.html#167953723">(Jun 12 2019 at 14:45)</a>:</h4>
<p>to force <code>y</code> to become unusable.</p>



<a name="167954235"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Explicit%20move%20for%20structs/near/167954235" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Explicit.20move.20for.20structs.html#167954235">(Jun 12 2019 at 14:51)</a>:</h4>
<p>seems niche enough that at best it should be an intrinsic imo</p>



<a name="167955758"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Explicit%20move%20for%20structs/near/167955758" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Explicit.20move.20for.20structs.html#167955758">(Jun 12 2019 at 15:05)</a>:</h4>
<p><a href="https://stackoverflow.com/q/31168589/155423" target="_blank" title="https://stackoverflow.com/q/31168589/155423">How to force a move of a type which implements the Copy trait?</a></p>
<p>I've never understood the question either.</p>



<a name="167956281"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Explicit%20move%20for%20structs/near/167956281" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Explicit.20move.20for.20structs.html#167956281">(Jun 12 2019 at 15:11)</a>:</h4>
<p><span class="user-mention" data-user-id="124288">@oli</span> seems niche enough to not even be an intrinsic imo <span aria-label="slight smile" class="emoji emoji-1f642" role="img" title="slight smile">:slight_smile:</span></p>



<a name="167956427"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Explicit%20move%20for%20structs/near/167956427" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Explicit.20move.20for.20structs.html#167956427">(Jun 12 2019 at 15:12)</a>:</h4>
<p>agreed, I was trying to set an upper limit</p>



<a name="167956471"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Explicit%20move%20for%20structs/near/167956471" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Explicit.20move.20for.20structs.html#167956471">(Jun 12 2019 at 15:12)</a>:</h4>
<p>I think that (absent any other context), the right thing would be to make a newtype. This type wants different semantics from the underlying <code>Copy</code> type.</p>



<a name="167956522"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Explicit%20move%20for%20structs/near/167956522" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Explicit.20move.20for.20structs.html#167956522">(Jun 12 2019 at 15:13)</a>:</h4>
<p>e.g. <code>i32</code> can be copied, but an <code>Identifer(i32)</code> cannot</p>



<a name="167956569"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Explicit%20move%20for%20structs/near/167956569" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Explicit.20move.20for.20structs.html#167956569">(Jun 12 2019 at 15:13)</a>:</h4>
<p>Yea, Rust encodes most-if-not-all features in the type</p>



<a name="167959056"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Explicit%20move%20for%20structs/near/167959056" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Explicit.20move.20for.20structs.html#167959056">(Jun 12 2019 at 15:37)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> oh no; this is where an ECS starts =P</p>



<a name="167959220"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Explicit%20move%20for%20structs/near/167959220" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Explicit.20move.20for.20structs.html#167959220">(Jun 12 2019 at 15:38)</a>:</h4>
<p><span class="user-mention" data-user-id="126931">@centril</span> entity-component system? I'm not seeing the connection...</p>



<a name="167960170"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Explicit%20move%20for%20structs/near/167960170" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Explicit.20move.20for.20structs.html#167960170">(Jun 12 2019 at 15:47)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span>  <code>struct DefId(...);</code> and stuff</p>



<a name="167960184"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Explicit%20move%20for%20structs/near/167960184" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Explicit.20move.20for.20structs.html#167960184">(Jun 12 2019 at 15:47)</a>:</h4>
<p>newtyping an integer like that</p>



<a name="167961468"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Explicit%20move%20for%20structs/near/167961468" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Explicit.20move.20for.20structs.html#167961468">(Jun 12 2019 at 16:00)</a>:</h4>
<p>also as far as the typechecker goes, intrinsics are just functions, right? So doesnt seem like a job for an intrinsic to me.</p>



<a name="167961535"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Explicit%20move%20for%20structs/near/167961535" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Explicit.20move.20for.20structs.html#167961535">(Jun 12 2019 at 16:00)</a>:</h4>
<p>if it's magic in the type system it <em>is</em> a syntactic language primitive. papering over that by pretending it is an intrinsic doesnt help anyone IMO.</p>



<a name="167962073"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Explicit%20move%20for%20structs/near/167962073" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Explicit.20move.20for.20structs.html#167962073">(Jun 12 2019 at 16:04)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> agreed</p>



<a name="167962153"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Explicit%20move%20for%20structs/near/167962153" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Explicit.20move.20for.20structs.html#167962153">(Jun 12 2019 at 16:05)</a>:</h4>
<p>If you just want a variable to be unreadable (though not e.g. a field, I'm not sure we could support that, you can always do <code>let y = x; let x;</code> presumably?</p>



<a name="167962187"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Explicit%20move%20for%20structs/near/167962187" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Explicit.20move.20for.20structs.html#167962187">(Jun 12 2019 at 16:05)</a>:</h4>
<p>intrinsics are magic in terms of their <em>dynamic semantics</em> (so they are primitives in that sense), but for the <em>static semantics</em> they should be like any odd function. <code>transmute</code> is an exception already (it checks that the sizes are the same) and that already has some annoying consequences (like you can't wrap it).</p>



<a name="167962270"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Explicit%20move%20for%20structs/near/167962270" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Explicit.20move.20for.20structs.html#167962270">(Jun 12 2019 at 16:06)</a>:</h4>
<blockquote>
<p>transmute is an exception already (it checks that the sizes are the same) and that already has some annoying consequences (like you can't wrap it).</p>
</blockquote>
<p>Hopefully temporary with some const generics</p>



<a name="167962328"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Explicit%20move%20for%20structs/near/167962328" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Explicit.20move.20for.20structs.html#167962328">(Jun 12 2019 at 16:06)</a>:</h4>
<p><span class="user-mention" data-user-id="116122">@simulacrum</span> </p>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">foo</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">x</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="p">;</span><span class="w"> </span><span class="c1">// error; unknown type.</span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="167962447"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Explicit%20move%20for%20structs/near/167962447" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Explicit.20move.20for.20structs.html#167962447">(Jun 12 2019 at 16:08)</a>:</h4>
<p><code>let x: ();</code>?</p>



<a name="167962457"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Explicit%20move%20for%20structs/near/167962457" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Explicit.20move.20for.20structs.html#167962457">(Jun 12 2019 at 16:08)</a>:</h4>
<p>you can do:</p>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">foo</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">0</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">x</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">();</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="167962475"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Explicit%20move%20for%20structs/near/167962475" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Explicit.20move.20for.20structs.html#167962475">(Jun 12 2019 at 16:08)</a>:</h4>
<p><span class="user-mention" data-user-id="116122">@simulacrum</span> yeah but you need to initialize it also</p>



<a name="167962496"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Explicit%20move%20for%20structs/near/167962496" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Explicit.20move.20for.20structs.html#167962496">(Jun 12 2019 at 16:08)</a>:</h4>
<p>what for? if you're specifically intending to never read from it</p>



<a name="167962504"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Explicit%20move%20for%20structs/near/167962504" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> centril <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Explicit.20move.20for.20structs.html#167962504">(Jun 12 2019 at 16:08)</a>:</h4>
<p>or not, if you don't use it; but now you need <code>#[allow(unused)]</code></p>



<a name="167962540"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Explicit%20move%20for%20structs/near/167962540" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Explicit.20move.20for.20structs.html#167962540">(Jun 12 2019 at 16:09)</a>:</h4>
<p>The unused lint will get in your way even if you initalize.</p>



<a name="167962542"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Explicit%20move%20for%20structs/near/167962542" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Explicit.20move.20for.20structs.html#167962542">(Jun 12 2019 at 16:09)</a>:</h4>
<p>sure, yeah, there's no real way to shadow w/o allow(unused) for this purpose (initializing it won't help)</p>



<a name="167966210"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Explicit%20move%20for%20structs/near/167966210" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Explicit.20move.20for.20structs.html#167966210">(Jun 12 2019 at 16:53)</a>:</h4>
<p>I do think a <code>move {}</code> block would fit nicely in terms of filling out the syntax grid, but it's kind of silly (and plain <code>{}</code> does that now in most cases)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>